{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "81fa44c1-b807-4072-b1f6-59508fcb7b4e",
   "metadata": {},
   "source": [
    "Chapter 05\n",
    "\n",
    "# 切比雪夫距离\n",
    "Book_6《数据有道》 | 鸢尾花书：从加减乘除到机器学习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ed2d3838-4a0b-4436-9d35-66591c471fb9",
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.spatial import distance\n",
    "from sklearn.metrics import pairwise_distances"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "224d94b5-853b-45c8-a3d6-48bc5c2159b9",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Sample data points\n",
    "X = [[-5, 0], [4, 3], [3, -4]]\n",
    "\n",
    "# Query point\n",
    "q = [0, 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9481cc32-1e71-4cef-b4cd-ba63553f3b8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Compute the Chebyshev distance.\n",
    "d_1 = distance.chebyshev(q, X[0])\n",
    "d_2 = distance.chebyshev(q, X[1])\n",
    "d_3 = distance.chebyshev(q, X[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8f7ebd77-908f-4801-a18d-e48f39d0df44",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Pairwise Chebyshev distances between X and q\n",
      "[[5.]\n",
      " [4.]\n",
      " [4.]]\n"
     ]
    }
   ],
   "source": [
    "# pairwise distances between rows of X and q\n",
    "dst_pairwise_X_q = pairwise_distances(X, [q], metric='chebyshev')\n",
    "print('Pairwise Chebyshev distances between X and q')\n",
    "print(dst_pairwise_X_q)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
